var currentPage = 1;
var pageSize = 10;
// 获取token
var token = sessionStorage.getItem("token");
// var baseurl = "http://120.26.176.136:7788";
var baseurl = "http://localhost:7788";
// 1.请求所有用户角色-设置到搜索下拉框
$.ajax({
  url: baseurl + "/role/findAll",
  type: "get",
  headers: {
    Authorization: sessionStorage.getItem("token"),
  },
  success: function (data) {
    console.log(data);
    data.data.forEach((item) => {
      $(".container-fluid .user-val").after(
        `<option>` + item.name + `</option>`
      );
    });
  },
});

// 2.封装请求所有的用户数据函数
function findAllByPage(currentPage, pageSize, username, rolename) {
  if (username != "") {
    url =
      baseurl +
      "/baseUser/pageQuery?page=" +
      currentPage +
      "&pageSize=" +
      pageSize +
      "&username" +
      username;
  } else if (rolename != "请选择角色") {
    url =
      baseurl +
      "/baseUser/pageQuery?page=" +
      currentPage +
      "&pageSize=" +
      pageSize +
      "&rolename" +
      rolename;
  } else if (username != "" && rolename != "请选择角色") {
    url =
      baseurl +
      "/baseUser/pageQuery?page=" +
      currentPage +
      "&pageSize=" +
      pageSize +
      "&username" +
      username +
      "&rolename" +
      rolename;
  } else {
    // 均为空时
    url =
      baseurl +
      "/baseUser/pageQuery?page=" +
      currentPage +
      "&pageSize=" +
      pageSize;
  }
  $.ajax({
    url: url,
    type: "get",
    headers: {
      Authorization: sessionStorage.getItem("token"),
    },
    success: function (data) {
      console.log(data, "----");
      data.data.list.forEach((item) => {
        $(".user-tables tbody").append(`
<tr>  
<td>${item.id}</td >
<td>${item.username}</td >
<td>${item.realname}</td >
<td>${item.roles
          .map(function (it) {
            return it.name;
          })
          .join()}</td >
<td>${item.gender === "male" ? "男" : "女"}</td>
<td>${item.status}</td >
<td>${item.telephone}</td >
<td>
<button>设置</button>
<button>移除</button>
<button>详情</button>
<button>修改</button>
</td>
</tr> 
`);
      });
    },
  });
}
findAllByPage(currentPage, pageSize);

// 根据所有用户个数，设置分页按钮个数
$.ajax({
  url: baseurl + "/baseUser/findAll",
  type: "get",
  headers: {
    Authorization: sessionStorage.getItem("token"),
  },
  success: function (data) {
    console.log(data, "+++++");
    var num = Math.ceil(data.data.length / pageSize);
    // console.log(num);
    for (var i = num; i > 0; i++) {
      console.log(i);
      $(".num").after(`<li class="currentnum"><a href="#">${i + 1}</a></li>`);
    }
  },
});

// 设置分页（根据用户点击当前事件分页按钮，重新查询当前页面的所有用户）
$(".currentnum").click(function () {
  var currentPage = currentPage;
  console.log(currentPage);
  $.ajax({
    url: baseurl + "/baseUser/pageQuery",
    type: "get",
    headers: {
      Authorization: sessionStorage.getItem("token"),
    },
    success: function (data) {
      console.log(data, "======");
    },
  });
});

// 根据用户输入内容查询数据
$(".user-ipt").click(function () {
  var username = $("input").val();
  var rolename = $("select").find("option:selected").val();
  // console.log(username, rolename);
  if (username && rolename) {
    $.ajax({
      url: baseurl + "/baseUser/pageQuery",
      type: "get",
      headers: {
        Authorization: sessionStorage.getItem("token"),
      },
      data: {
        username: username ? username : "",
        rolename: rolename ? rolename : "",
      },
      success: function (data) {
        console.log(data);
      },
    });
  }
});
